<?php
/**
 * 记录日志
 * +-----------------------------
 * User: BOBO
 * +-----------------------------
 * Date: 2021/10/9
 * +-----------------------------
 * Time: 14:59
 * +-----------------------------
 * Created by PhpStorm.
 * +-----------------------------
 * Copyright (c) 2020~2031
 * +-----------------------------
 */

namespace PDDCore;


class TopLogger
{
    /**
     * 基础配置
     * @var array
     */
    public $conf = array(
        "separator" => "\t",
        "log_file" => ""
    );

    /**
     * 文件
     * @var
     */
    private $fileHandle;

    /**
     * 记录日志
     * @param $logData
     * @return bool
     */
    public function log($logData)
    {
        if ("" == $logData || array() == $logData)
        {
            return false;
        }
        if (is_array($logData))
        {
            $logData = implode($this->conf["separator"], $logData);
        }
        $logData = $logData. "\n";
        fwrite($this->getFileHandle(), $logData);
    }

    /**
     * 文件地址
     * @return bool|resource
     */
    protected function getFileHandle()
    {
        if (null === $this->fileHandle)
        {
            if (empty($this->conf["log_file"]))
            {
                trigger_error("no log file spcified.");
            }
            $logDir = dirname($this->conf["log_file"]);
            if (!is_dir($logDir))
            {
                mkdir($logDir, 0777, true);
            }
            $this->fileHandle = fopen($this->conf["log_file"], "a");
        }
        return $this->fileHandle;
    }

}